"use strict";


function new_task(item){
	var new_dom_id = unique_dom_id++;
	var htmlstr='<div  data-taskid="'+item.id+'" ';
	htmlstr+=' class="task getby_search " id="'+new_dom_id+'"';
	htmlstr=htmlstr
	+' data-sort="'+item.sort+'"'
	+' data-stage="'+item.stage+'"'
	+' data-archive="'+item.archive+'"'
	+' data-getway="'+item.getway+'"'
	+' data-intags="'+item.intags+'"'
	+' data-tags="'+item.tags+'"';
	htmlstr+='>';
 //   htmlstr+='<div >';
//	console.log(htmlstr);
	htmlstr+=new_task_header(item)+'</div>';
	return htmlstr;
	}
	
function new_task_header(item){
	var htmlstr='';
	
//	+'<i  class="fa '+node_type_icon[item.nodetype]+'"></i>'	
//	htmlstr+='<span class="task_title task_for_drag bounceIn animated">'+item.title+'</span>';
    htmlstr+='<div class="task_header">';
    htmlstr+='<a class="expend_fold" href="###" ><i  class="fa fa-circle"></i></a>';
	htmlstr+='<span class="task_title task_for_drag">'+item.title+'</span>';
    htmlstr+='<span class="iconbar">'
    htmlstr+='<a class="expend_fold" href="###" ><i  class="fa"></i></a>';
    //	htmlstr+='<span class="task_stage"><i  class="fa '+stage_icon[item.stage]+'"></i></span>';//使用font-awesome的字体图标
	htmlstr+='<a class="stage_icon"><i></i></a>';//使用样式表图形文件图标
//	htmlstr+='<img class="stage_icon" src="'+stage_svg_icon[item.stage]+'" >';//使用图标数组

	if(item.internal_link>0){
		htmlstr=htmlstr+'<a class="task_internal_link" href="###" data-internal_link="'+item.internal_link+'" title="关联到其它任务"><i class="fa fa-share-square" onclick="position_task_id('+item.internal_link+')"></i></a>';
	}else{
		htmlstr+='<a class="task_internal_link"></a>';
	}
	
	if(item.link){
		let r=item.link.match(/http[s]?\:\/\/([^\/]*)/);
//		console.log(r); 
		if(r){
//			console.log(r[0]);
			htmlstr=htmlstr+'<a class="task_link" href="'+item.link+'" target="_blank" title="外链" ><img  class="fa fa-link" src="'+r[0]+'/favicon.ico'+'" onerror="this.src=\'/_assets/icons/link.svg\';"></a>';
		}else{
			htmlstr=htmlstr+'<a class="task_link" href="'+item.link+'" target="_blank" title="外链"><i class="fa fa-link"></i></a>';			
		}
	}else{
		htmlstr+='<a class="task_link"></a>';
	}
	if(item.comment){
		htmlstr=htmlstr+'<a class="task_comment_icon" href="###"  title="备注"><i class="fa fa-comment"></i></a>';
	}else{
		htmlstr+='<a class="task_comment_icon"></a>';
		item.comment='';
	}
	if(item.photo){
		htmlstr=htmlstr+'<a class="task_photo_icon" href="###" title="图片" data-url="'+item.photo+'"><i class="fa fa-photo"></i></a>';
	}else{
		htmlstr+='<a class="task_photo_icon"></a>';
	}	
	htmlstr+='<a class="magic_icon"><i class="fa"></i></a>';
    htmlstr+='</span>';
    htmlstr+='</div>';
    
//	htmlstr+='<a class="icon_button btn_for_toolbar"  href="###"  title="操作面板'+item.id+'"><i  class="fa fa-lg fa-angle-double-right"></i></a>';
	
//	htmlstr+='<div class="task_photo rotateIn animated " ><img src='+item.photo+' style="max-width:100%"></div>';
	htmlstr+='<div class="task_photo" ><img src='+item.photo+' style="max-width:100%"></div>';
	htmlstr+='<div class="task_comment " >'+html_encode(item.comment)+'</div>';
	
	return htmlstr;
}		
		
	
function new_task_header2(item){
	var htmlstr='';
//	htmlstr+='<div class="task_info_btn_bar">';
	
	
//	htmlstr+='<span class="task_stage"><i  class="fa '+stage_icon[item.stage]+'"></i></span>';//使用font-awesome的字体图标
	htmlstr+='<a class="stage_icon"><i></i></a>';//使用样式表图形文件图标
//	htmlstr+='<img class="stage_icon" src="'+stage_svg_icon[item.stage]+'" >';//使用图标数组
	
	htmlstr+='<a class="expend_fold" href="###" title="展开"><i  class="fa fa-folder-o"></i></a>'
	// if(item.start_date){
		// htmlstr+='<span class="start_date">&nbsp;'+String(item.start_date).substring(5,10)+'&nbsp;</span>';
	// }else{
		// htmlstr+='<span class="start_date"></span>';
	// }

	if(item.internal_link>0){
		htmlstr=htmlstr+'<a class="task_internal_link" href="###" data-internal_link="'+item.internal_link+'" title="关联到其它任务"><i class="fa fa-share-square" onclick="position_task_id('+item.internal_link+')"></i></a>';
	}else{
		htmlstr+='<a class="task_internal_link"></a>';
	}
	
	if(item.link){
		r=item.link.match(/http[s]?\:\/\/([^\/]*)/);
//		console.log(r); 
		if(r){
//			console.log(r[0]);
			htmlstr=htmlstr+'<a class="task_link" href="'+item.link+'" target="_blank" title="外链" ><img  class="fa fa-link" src="'+r[0]+'/favicon.ico'+'" onerror="this.src=\'/_assets/icons/link.svg\';"></a>';
		}else{
			htmlstr=htmlstr+'<a class="task_link" href="'+item.link+'" target="_blank" title="外链"><i class="fa fa-link"></i></a>';			
		}
	}else{
		htmlstr+='<a class="task_link"></a>';
	}
	if(item.comment){
		htmlstr=htmlstr+'<a class="task_comment_icon" href="###"  title="备注"><i class="fa fa-comment"></i></a>';
	}else{
		htmlstr+='<a class="task_comment_icon"></a>';
		item.comment='';
	}
	
//	htmlstr+='</div>';
	htmlstr+='<div class="task_title">'+item.title+'</div>';
	htmlstr+='<div class="task_pic_text_area">';
	htmlstr+='<div class="task_picture radius" >'+item.picture+'</div>';
	htmlstr+='<div class="task_comment radius" >'+item.comment+'</div>';
	htmlstr+='</div>';
	return htmlstr;
}
function refresh_all_and_append(){
		var loading=layer.msg('正从服务器上加载数据，请稍候！', {icon: 16});
//		var loading = layer.load(2); //换了种风格

		var items;
		
		$.getJSON("/index.php/tasktree/getallchild/"+$("#parm").attr("data-root"), 
			function(dataGet) { 
//			console.log(dataGet);
				if(dataGet.data){
//					console.log(dataGet.data);
					items=dataGet.data;
						$.each(items,function(idx,item){ //生成所有任务，先挂装到不分类任务下
                            $("#showarea").append(new_task(item));
						});
						//$("#temp div.task").hide();
                        $.each(items,function(idx,item){ //所有任务依据自己的parnet挂装
							console.log(item);
							$(".task[data-taskid="+item.parent+"]").append($(".task[data-taskid="+item.id+"]"));
							$(".task[data-taskid="+item.parent+"]").append($(".task[data-taskid="+item.id+"]"));
						});	
						
					

					//$('#btn_refresh_all i').removeClass('fa-spin');
//					$("#showarea div.task").hide();
					$("#showarea>div.task").show();
					$("#showarea").append($("#endpage"));
					
					expend_icon_status();
					//创建页号
					create_sort();
					//设置标题
					document.title=$("#showarea div.task").first().children(".task_title").text();
					//动态设置背景（通过动态增加类的属性）
					var bgimg=$("#showarea div.task").first().children(".task_photo").children("img").attr("src");
					console.log(bgimg);
					document.styleSheets[0].insertRule('.page_view .current_page {background-image:url("'+bgimg+'");}', 0);
					document.styleSheets[0].insertRule('.page_view .current_page {background-size:100%', 0);
					//动态设置各部件的特效
					var intags=$("#showarea div.task").first().attr("data-intags");
					console.log(intags);
					if(intags){
						var intags_arr=intags.split(" ");
						for (var i in intags_arr) {
							console.log(intags_arr[i]);
							var a=intags_arr[i].split('-');
							console.log(a);
							if(a[0]=='animated'){
								switch (a[1]){
                                    case 'header':
									case 'photo':
									case 'title':
									case 'comment':
                                        console.log(a[1]);
										$("#showarea div.task").find(".task_"+a[1]).addClass('animated');
										$("#showarea div.task").find(".task_"+a[1]).addClass(a[2]);
										break;
									case 'task'://设置当前页总体的特效
										$("#showarea div.task").find(".task").addClass('animated');
										document.styleSheets[0].insertRule('.page_view .current_page {animation-name:'+a[2]+';}', 0);
								}
							}
						}
					}
					
//					$("div.task[data-intags~='animated-photo']").childrens(".task_photo").addClass('animated');
//					console.log($("div.task[data-intags|='animated']").attr("id"));
					
					//tags=
//					$("div.task[data-intags|='animated-photo']").childrens(".task_photo").addClass('animated');
//					$("div.task[data-intags|='animated-comment']").childrens(".task_comment").addClass('animated');

					//$(".task[data-taskid=1]>div.task").show();
					//$(".task[data-taskid=2]>div.task").show();
					
					//关闭
//					layer.close(index); 
					layer.close(loading); 
					
					// $("#temp").children("div.task").first().addClass("current_page");
					// current_domid=$("#listarea").children("div.task").first().attr("id");
					// console.log($("#listarea").children("div.task").first()); 	
					disp_current_page();
				}
			}		
		);
		$('#btn_refresh_all i').addClass('fa-spin');	
 
}		
function expend_icon_status(){
//	$('div.task a.expend_fold i').css("color",'');
//	$('div.task a.expend_fold i').addClass("color",'');
//	$('div.task').parents('div.task').children(".task_header").children("a.expend_fold").children("i").addClass('fa-folder-o');
	$('div.task').parents('div.task').children(".task_header").children("a.expend_fold").children("i").removeClass('fa-circle').addClass('fa-folder-o');;
 //   $('div.task').parents('div.task').children(".task_header").children(".iconbar").children("a.expend_fold").children("i").addClass('fa-folder-o');
}
function expend_fold_by_domid(domid){
	if($("#"+domid+">a.expend_fold i").hasClass('fa-folder-o')){
		$("#"+domid).children('div.task').show(200);
		$("#"+domid).children('a.expend_fold').children('i').removeClass('fa-folder-o');
		$("#"+domid).children('a.expend_fold').children('i').addClass('fa-folder-open-o');
		$("#"+domid).children('a.expend_fold').attr('title','折叠');
	}else{
		$("#"+domid).children('div.task').hide(200);
		$("#"+domid).children('a.expend_fold').children('i').addClass('fa-folder-o');
		$("#"+domid).children('a.expend_fold').children('i').removeClass('fa-folder-open-o');
		$("#"+domid).children('a.expend_fold').attr('title','展开');
	}
 }
function create_sort(){
	$('div.task').each(function (i){
		console.log(i);
		i++;
		$(this).attr("data-page",i);
	});
	$('#page_sum').text($('div.task').length);
}
$(document).ready(function() {

    //一键取得所有网址书签并挂装
	// refresh_all_and_append();
	get_data_and_append("/index.php/task/geturlbook/","#urlbook",13,'urlbook');
	
//	$("div.task").next("div.task").children("a").attr("");
	
 	$("body").delegate('a.task_comment_icon','click',function(){
		$(event.target).closest('div.task').children('.task_comment').toggleClass('z-visualise');
		$(event.target).closest('div.task').removeClass('z-detail');
		$(".z-visualise").closest('div.task').addClass('z-detail');	
	});
	 $("body").delegate('a.task_photo_icon','click',function(){
		$(event.target).closest('div.task').children('.task_photo').toggleClass('z-visualise');
		$(event.target).closest('div.task').removeClass('z-detail');
		$(".z-visualise").closest('div.task').addClass('z-detail');
	});
	
	$("#showarea").delegate('a.expend_fold','click',function(){
//			console.log(event.target);
			var domid=$(event.target).closest("div.task").attr("id");	
			if($("#showarea").hasClass("page_view")){
				if($(event.target).closest("i").hasClass("fa-folder-o")){
					$("#"+domid).addClass('z-detail');	
				}else{
					$("#"+domid).removeClass('z-detail');
					$(".z-visualise").closest('div.task').addClass('z-detail');
				}
			}
    if($(event.target).closest("i").hasClass('fa-folder-o')){
		$("#"+domid).children('div.task').show(200);
		$(event.target).closest("i").removeClass('fa-folder-o');
		$(event.target).closest("i").addClass('fa-folder-open-o');
		$(event.target).closest("i").attr('title','折叠');
	}else{
		$("#"+domid).children('div.task').hide(200);
		$(event.target).closest("i").addClass('fa-folder-o');
		$(event.target).closest("i").removeClass('fa-folder-open-o');
		$(event.target).closest("i").attr('title','展开');
	}
        
			//expend_fold_by_domid(domid);
	});

	$("body").delegate('.task_title','click',function(){
		current_domid=$(event.target).closest("div.task").attr("id");
		disp_current_page();
		$("#showarea").removeClass("tree_view");
		$("#showarea").addClass("page_view");
/* 		$("div.task.current_page").removeClass("current_page");
		$("#"+domid).addClass("current_page");
//		$("#"+domid).show();
//		$("#"+domid).parents("div.task").show();
//		$("#"+domid).parents("div.task").children("span").hide();
		
			var newpage=Number($("#"+current_domid).attr("data-page"))-1;
	current_domid=$("div.task[data-page="+newpage+"]").attr("id");
	 */		
	});
		$("body").delegate('.btn_tree_view','click',function(){
		var domid=$(event.target).parents("div.task").first().attr("id");
//		$("div.task").hide();
		$("div.task.current_page").removeClass("current_page");
		$("#"+domid).addClass("current_page");
//		$("#"+domid).show();
//		$("#"+domid).parents("div.task").show();
//		$("#"+domid).parents("div.task").children("span").hide();
		$("#showarea").toggleClass("page_view");
		$("#showarea").toggleClass("tree_view");
		
	});
});	//end of $(document).ready事件